package com.example.message.core;

import java.util.Set;
import java.util.TreeSet;

public class MessageSearchParameterBuilder {

	private Set<String> categories;
	private Set<String> subcategories;
	private Set<String> keywords;
	private Set<String> authors;
	private Set<String> languages;
	private Set<String> codes;
	private Double minVersion;
	private Double maxVersion;
	
	private MessageSearchParameterBuilder() {
		categories = new TreeSet<>();
		subcategories = new TreeSet<>();
		keywords = new TreeSet<>();
		authors = new TreeSet<>();
		languages = new TreeSet<>();
		codes = new TreeSet<>();
		minVersion = Double.MIN_VALUE;
		maxVersion = Double.MAX_VALUE;
	}
	
	public static final MessageSearchParameterBuilder createBuilder() {
		return new MessageSearchParameterBuilder();
	}
	
	public MessageSearchParameterBuilder category(String category) {
		this.categories.add(category);
		return this;
	}
	
	public MessageSearchParameterBuilder categories(String... categories) {
		if (categories != null && categories.length > 0) {
			for (String category : categories) {
				this.categories.add(category);
			}
		}
		return this;
	}
	
	protected Set<String> getCategories() {
		return categories;
	}

	public MessageSearchParameterBuilder subcategory(String subcategory) {
		this.subcategories.add(subcategory);
		return this;
	}
	
	public MessageSearchParameterBuilder subcategories(String... subcategories) {
		if (subcategories != null && subcategories.length > 0) {
			for (String subcategory : subcategories) {
				this.subcategories.add(subcategory);
			}
		}
		return this;
	}
	
	protected Set<String> getSubcategories() {
		return subcategories;
	}

	public MessageSearchParameterBuilder keyword(String keyword) {
		this.keywords.add(keyword);
		return this;
	}
	
	public MessageSearchParameterBuilder keywords(String... keywords) {
		if (keywords != null && keywords.length > 0) {
			for (String keyword : keywords) {
				this.keywords.add(keyword);
			}
		}
		return this;
	}
	
	protected Set<String> getKeywords() {
		return keywords;
	}

	public MessageSearchParameterBuilder author(String author) {
		this.authors.add(author);
		return this;
	}
	
	public MessageSearchParameterBuilder authors(String... authors) {
		if (authors != null && authors.length > 0) {
			for (String author : authors) {
				this.authors.add(author);
			}
		}
		return this;
	}
	
	protected Set<String> getAuthors() {
		return authors;
	}
	
	public MessageSearchParameterBuilder language(String language) {
		this.languages.add(language);
		return this;
	}
	
	public MessageSearchParameterBuilder languages(String... languages) {
		if (languages != null && languages.length > 0) {
			for (String language : languages) {
				this.languages.add(language);
			}
		}
		return this;
	}
	
	protected Set<String> getLanguages() {
		return languages;
	}
	
	public MessageSearchParameterBuilder code(String code) {
		this.codes.add(code);
		return this;
	}
	
	public MessageSearchParameterBuilder codes(String... codes) {
		if (codes != null && codes.length > 0) {
			for (String code : codes) {
				this.codes.add(code);
			}
		}
		return this;
	}
	
	protected Set<String> getCodes() {
		return codes;
	}
	
	public MessageSearchParameterBuilder minVersion(Double minVersion) {
		this.minVersion = minVersion;
		return this;
	}
	
	public MessageSearchParameterBuilder maxVersion(Double maxVersion) {
		this.maxVersion = maxVersion;
		return this;
	}
	
	protected Double getMaxVersion() {
		return maxVersion;
	}
	
	protected Double getMinVersion() {
		return minVersion;
	}
	
	public MessageSearchParameter build() {
		MessageSearchParameter msp = new MessageSearchParameter();
		msp.setAuthors(authors);
		msp.setCategories(categories);
		msp.setCodes(codes);
		msp.setKeywords(keywords);
		msp.setLanguages(languages);
		msp.setMaxVersion(maxVersion);
		msp.setMinVersion(minVersion);
		msp.setSubcategories(subcategories);
		
		return msp;
	}
	
//	public static void main(String[] args) {
//		MessageSearchParameter msp = MessageSearchParameterBuilder.createBuilder()
//			.author("hasan")
//			.categories("CRM", "MARKETING", "ACCOUNTING")
//			.languages("TR","EN","IT")
//			.minVersion(1.0)
//			.maxVersion(2.0)
//			.build();
//		System.out.println(msp.toString());
//	}
}
